﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Benlai.Model
{
    public class ResultInfo<T>
    {
        public ResultInfo() { }
        public ResultInfo(int code, string msg)
        {
            m_code = code;
            m_msg = msg;
        }
        public ResultInfo(int code, string msg, T value)
        {
            m_code = code;
            m_msg = msg;
            m_value = value;
        }

        private int m_code = -100;
        public int Code { get { return m_code; } set { m_code = value; } }

        private string m_msg = "失败";
        public string Msg { get { return m_msg; } set { m_msg = value; } }

        private T m_value = default(T);
        public T Value { get { return m_value; } set { m_value = value; } }

        public override string ToString()
        {
            return string.Format("Code:{0},Msg:{1}", m_code, m_msg);
        }


        public static ResultInfo<T> GetServerOutResultInfo(string msg = "")
        {
            return new ResultInfo<T>((int)AppEnum.ReturnCode.ServerOut, "无法找到服务." + msg);
        }
        public static ResultInfo<T> GetTimeOutResultInfo(string msg = "")
        {
            return new ResultInfo<T>((int)AppEnum.ReturnCode.TimeOut, "超时." + msg);
        }
        public static ResultInfo<T> GetSaveFailureResultInfo(string msg = "")
        {
            return new ResultInfo<T>((int)AppEnum.ReturnCode.SaveFailure, "保存失败." + msg);
        }
        public static ResultInfo<T> GetCheckFailureResultInfo(string msg = "")
        {
            return new ResultInfo<T>((int)AppEnum.ReturnCode.CheckFailure, "验证失败." + msg);
        }
        public static ResultInfo<T> GetFailureResultInfo(string msg = "")
        {
            return new ResultInfo<T>((int)AppEnum.ReturnCode.Failure, "失败." + msg);
        }
        public static ResultInfo<T> GetPartialSuccessResultInfo(T value, string msg = "")
        {
            return new ResultInfo<T>((int)AppEnum.ReturnCode.PartialSuccess, "部分成功.", value);
        }
        public static ResultInfo<T> GetSuccessResultInfo(T value, string msg = "")
        {
            return new ResultInfo<T>((int)AppEnum.ReturnCode.Success, "成功.", value);
        }
        public static ResultInfo<T> NoObject(T value, string msg = "")
        {
            msg = string.IsNullOrEmpty(msg) ? "不存在对象" : msg;
            return new ResultInfo<T>((int)AppEnum.ReturnCode.NoObject, msg, value);
        }
    }
}
